<?php
/**
 * Created by PhpStorm.
 * User: 小夏
 * Date: 2016/10/11 0011
 * Time: 9:39
 */
namespace Invest\Controller;
use Common\Controller\HomebaseController;
class DetailedController extends HomebaseController
{
    protected $course;
    protected $activity;
    protected $user;
    protected $holdings;
    protected $assetaccount;
    protected $ordergoods;
    protected $orderinfo;
    function _initialize()
    {
        parent::_initialize();
        $this->course = M("Course");
        $this->activity = M("Activity");
        $this->user = M("Users");
        $this->holdings = M("Holdings");
        $this->assetaccount = M("AssetAccount");
        $this->ordergoods = M("OrderGoods");
        $this->orderinfo = M("OrderInfo");
    }
    //金融学吧详情
    function decourse(){

        $id = I('get.id');
        $list = $this->course->where(array('id'=>$id))->find();
        $teaname= M('Users')->where(array('id'=>$list['uid']))->getField('id,user_nicename,avatar,signature',true);
        $number = $this->course->where(array('uid'=>$list['uid']))->sum('buy_coun');

        $list['uname'] = $teaname[$list['uid']]['user_nicename'];
        $list['avatar'] = $teaname[$list['uid']]['avatar'];
        $list['signature'] = $teaname[$list['uid']]['signature'];
        $list['number'] = $number;
        //查看是否购买过改视频
        $user = $_SESSION['user'];
        if($user){
            $result = $this->orderinfo
                ->alias('e')
                ->field('e.*,o.*,e.order_id as order_ide')
                ->join('LEFT JOIN __ORDER_GOODS__ o ON o.order_id=e.order_id')
                ->where(array('o.goods_id'=>$id,'o.goods_type'=>1,'e.user_id'=>$user['id']))
                ->select();
            $flag = 0;//0表示未支付
            foreach ($result as $value){
                if ($value['order_status'] == 1 && $value['pay_status'] == 1 )
                {
                    $flag = 1;
                }
            }
            if ($flag){
                $this->assign('play','pass');
            }
        }

        $smeta=json_decode($list['smeta'],true);
        $this->assign($list);
        $this->assign("smeta",$smeta);
        $this->display(":course");
    }
    //活动详情
    function deactivity(){
        $id = I('get.id');
        $list = $this->activity->where(array('id'=>$id))->find();
        $teaname= M('Users')->where(array('id'=>$list['uid']))->getField('id,user_nicename,avatar,signature',true);
        $list['uname'] = $teaname[$list['uid']]['user_nicename'];
        $list['avatar'] = $teaname[$list['uid']]['avatar'];
        $list['signature'] = $teaname[$list['uid']]['signature'];

        $smeta=json_decode($list['smeta'],true);
        $this->assign($list);
        $this->assign("smeta",$smeta);
        $this->display(":deactivity");
    }
    //视频播放
    function videoplay(){
        $id = I("get.id");
        if($id){
            if(sp_is_user_login()){
                $result = $this->course->where(array('id'=>$id))->find();

                if(empty((float)$result['shop_price'])){
                    $this->assign('url',$result['videourl']);
                }else{
                    $user = $_SESSION['user'];
                    //查找是否存在订单
                    $connter = $this->ordergoods
                        ->alias('o')
                        ->join('LEFT JOIN __ORDER_INFO__ r ON r.order_id=o.order_id')
                        ->where(array('o.goods_id'=>$id,'o.goods_type'=>1,'r.user_id'=>$user['id']))
                       // ->order('o.rec_id desc')
                        ->select();
                    $flag = 0;//0表示未支付
                    foreach ($connter as $k=>$value){
                        if($value['order_status'] == 1 && $value['pay_status'] == 1){
                            $flag =  1;
                        }
                    }
                    if ($flag){
                        $this->assign('url',$result['videourl']);
                    }else{
                       $this->error('您还未购买！',U('Invest/Detailed/decourse',array('id'=>$id)),2);
                    }

                }
            }else{
                $this->error('您还未登录！',U('Invest/Detailed/decourse',array('id'=>$id)),2);
            }

            $this->display(":video");
        }else{
            $this->error('非法操作！',U('Invest/List/finance',array('id'=>$id)),2);
        }
    }
    function test(){
        // 缓存初始化
        $Cache =S(array('type'=>'Redis','expire'=>60));

        if($Cache->name){
         //  unset($Cache->name); // 删除缓存
            print_r($Cache->name);exit;
           // $this->assign();
        }else{

            $result = $this->course->where(array('id'=>1))->find();
            S('name',$result);
            print_r(1111);exit;
        }
    }
}